package net.linku.utils;

/**
 * @ClassName IdCardParserUtil
 * @Description: 身份证号码解析数据表
 * @Author SilenceTian
 * @Date 2024/1/2
 * @Version V1.0.2
 **/

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

public class IdCardParserUtil {

    // 正则表达式校验身份证号码格式
    public static boolean isValidIdCardFormat(String idCardCode) {
        // 此处使用了简化的正则表达式，实际应用中需要根据具体规范调整
        String regex = "\\d{17}[0-9Xx]";
        return Pattern.matches(regex, idCardCode);
    }

    // 获取生日
    public static String getBirthday(String idCard) throws ParseException {
        String birthdayStr = idCard.substring(10, 14);
        SimpleDateFormat sdf = new SimpleDateFormat("MMdd");
        Date birthdayDate = sdf.parse(birthdayStr);
        return sdf.format(birthdayDate);
    }
    // 获取出生日期
    public static Date getBirthdayGregorian(String idCard) throws ParseException {
        String birthdayStr = idCard.substring(6, 14);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date birthdayDate = sdf.parse(birthdayStr);
//        return sdf.format(birthdayDate);
        return birthdayDate;
    }

    // 获取性别
    public static String getGender(String idCard) {
        int genderCode = Integer.parseInt(idCard.substring(16, 17));
        return genderCode % 2 == 0 ? "女" : "男";
    }


    public static String getConstellation(String idCard) {
        if (idCard == null || idCard.length() != 18) {
            return "无效身份证号码";
        }
        // 获取出生日期
        String birthdayStr = idCard.substring(6, 14);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        try {
            Date birthday = dateFormat.parse(birthdayStr);
            // 获取月份和日期
            SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
            int month = Integer.parseInt(monthFormat.format(birthday));
            SimpleDateFormat dayFormat = new SimpleDateFormat("dd");
            int day = Integer.parseInt(dayFormat.format(birthday));
            // 根据月份和日期判断星座
            if ((month == 3 && day >= 21) || (month == 4 && day <= 19)) {
                return "白羊座";
            } else if ((month == 4 && day >= 20) || (month == 5 && day <= 20)) {
                return "金牛座";
            } else if ((month == 5 && day >= 21) || (month == 6 && day <= 21)) {
                return "双子座";
            } else if ((month == 6 && day >= 22) || (month == 7 && day <= 22)) {
                return "巨蟹座";
            } else if ((month == 7 && day >= 23) || (month == 8 && day <= 22)) {
                return "狮子座";
            } else if ((month == 8 && day >= 23) || (month == 9 && day <= 22)) {
                return "处女座";
            } else if ((month == 9 && day >= 23) || (month == 10 && day <= 23)) {
                return "天秤座";
            } else if ((month == 10 && day >= 24) || (month == 11 && day <= 22)) {
                return "天蝎座";
            } else if ((month == 11 && day >= 23) || (month == 12 && day <= 21)) {
                return "射手座";
            } else if ((month == 12 && day >= 22) || (month == 1 && day <= 19)) {
                return "摩羯座";
            } else if ((month == 1 && day >= 20) || (month == 2 && day <= 18)) {
                return "水瓶座";
            } else if ((month == 2 && day >= 19) || (month == 3 && day <= 20)) {
                return "双鱼座";
            } else {
                return "未知星座";
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return "无效日期";
        }
    }

}

